﻿function openModalWin(title,url,callback,width,height,nosave)
{
    if (!width) { width = parseInt($(window).width()*0.8,10); }
    if (!height) { height = parseInt($(window).height() * 0.8, 10); }
    if ($(".modal").length > 0) { $(".modal").remove();}
    var html='<div class="modal" tabindex="-1" role="dialog">' +
        '<div class="modal-dialog" role="document" style="width:'+width+'px">' +
        '   <div class="modal-content">' +
        '      <div class="modal-header">' +
        '         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
        '        <h4 class="modal-title">' + title + '</h4>' +
        '   </div>' +
        '  <div class="modal-body" style="height:' + height +'px">' +
        '     <iframe frameborder="0" scrolling="no" width="100%" height="100%" id="modalIframe" />' +
        '</div>' +
        '        <div class="modal-footer"><label class="label label-success" id="modalTip"></label>' +
        '           <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>' +
        '          <button type="button" class="btn btn-success' + (nosave==null?"":" hide") + '"  id="modal-save-btn">保存</button>' +
        '     </div>' +
        '</div>' +
        '</div>' +
        '</div>';
    $(html).appendTo($(document.body)).modal().find("iframe").attr("src", url + (url.indexOf("?") > -1 ? "&" : "?") + "_t=" + Math.random());
    $('.modal').on('hidden.bs.modal',
        function(e) {
            if (callback) {
                callback();
            }
        });
    //绑定保存事件
    $("#modal-save-btn").click(function() {
        var modalResult = "";
        try {
            modalResult = $("#modalIframe")[0].contentWindow.Save();
        }
        catch (e) {
            modalResult = e.message;
        }
        if (modalResult != null && modalResult != "") {
            if (modalResult !="dnone") {
                $("#modalTip").html(modalResult);
            }
        }
        else {
            $(".modal").modal('toggle');
        }

    });
}
//获取Url中的参数内容
function GetUrlParam(name) {
    if (!name) { return ""; }
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        var hash = hashes[i].split('=');
        if (hash[0].toUpperCase() == name.toUpperCase()) {
            return hash[1];
        }
    }
    return "";
}
//转换时间格式
function formatDate(str) {
    if (!str || str.length < 8) return "";
    var d = new Date(str).getFullYear();
    if (isNaN(d)) return "";
    var y = str.substring(0, 4);
    if (y === "0000" || y === "0001") return "";
    str = str.replace(/T/g, ' ').replace(/Z/g, '');
    var index = str.lastIndexOf("\.");
    str = str.substring(0, index);
    return str;
}
